package com.blog.service.impl;

import com.blog.mapper.ArticleMapper;
import com.blog.pojo.Article;
import com.blog.pojo.ArticleWithAuthorNameVO;
import com.blog.pojo.PageBean;
import com.blog.service.ArticleService;
import com.blog.utils.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    // 分页查询所有文章
    @Override
    public PageBean listAll(Integer page, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(page,pageSize);
        // 执行分页查询
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listAll();
        // 获取分页结果
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        // 封装PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 查询单篇文章
    @Override
    public ArticleWithAuthorNameVO listById(Integer id) {
        ArticleWithAuthorNameVO articleWithAuthorNameVO = articleMapper.listById(id);
        return articleWithAuthorNameVO;
    }

    // 分页查询标签内所有文章
    @Override
    public PageBean listByTag(Integer tagId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByTag(tagId);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 分页查询关注的所有文章
    @Override
    public PageBean listByFocus(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Integer id = Integer.parseInt(UserContext.getUserId());
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByFocus(id);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 分页查看用户所有文章
    @Override
    public PageBean listByUserId(Integer id, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByUserId(id);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 分页查看当前登录用户所有文章，改
    @Override
    public PageBean listCurrentUser(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Integer id = Integer.parseInt(UserContext.getUserId());
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByUserId(id);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 分页查看当前登录用户感兴趣的所有文章，改
    @Override
    public PageBean listByHobbies(Integer page, Integer pageSize) {
        Integer id = Integer.parseInt(UserContext.getUserId());
        PageHelper.startPage(page,pageSize);
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByHobbies(id);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 分页查看搜索文章
    @Override
    public PageBean listByCondition(String condition, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleWithAuthorNameVO> articleList = articleMapper.listByCondition(condition);
        Page<ArticleWithAuthorNameVO> p = (Page<ArticleWithAuthorNameVO>) (articleList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 发布文章
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Article article, List<Integer> tagIds) {
        // System.out.println(UserContext.getUserId());
        int authorId = Integer.parseInt(UserContext.getUserId());
        article.setAuthorId(authorId);
        article.setCreated(LocalDateTime.now());
        article.setModified(LocalDateTime.now());
        if(article.getImage() == null) {
            article.setImage("https://blog240702.oss-cn-hangzhou.aliyuncs.com/ca077b4d-5de6-42fe-be6f-a07c272b1d03.jpg");  // 设置默认封面
        }
        article.setSupport(0);
        articleMapper.insert(article);

        Article newArticle = articleMapper.newsest(1);
        Integer articleId = newArticle.getId();

        for(Integer tagId: tagIds) {
            articleMapper.insertArticleTag(articleId, tagId);
        }


    }

    // 编辑文章
    @Override
    public void update(Article article) {
        article.setModified(LocalDateTime.now());
        if(article.getImage() == null) {
            article.setImage("https://blog240702.oss-cn-hangzhou.aliyuncs.com/ca077b4d-5de6-42fe-be6f-a07c272b1d03.jpg");
        }
        articleMapper.update(article);
    }

    // 批量删除文章
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        articleMapper.delete(ids);
        // 删除文章关联标签和文章评论，同时删掉点赞表中内容
        for(Integer id : ids) {
            articleMapper.deleteSupport(id);
            articleMapper.deleteCommentsByArticleId(id);
            articleMapper.deleteTagsByArticleId(id);
        }
    }

    // 根据查找文章实体
    @Override
    public Article findById(Integer id){
        return articleMapper.findById(id);
    }

    // 给文章点赞
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean support(Integer articleId) {
        Integer userId = Integer.parseInt(UserContext.getUserId());
        if(articleMapper.countThisSupport(userId, articleId) != 0) {
            return false;
        }
        Article article = findById(articleId);
        Integer sup = article.getSupport();
        sup = sup + 1;
        article.setSupport(sup);
        articleMapper.update(article);
        articleMapper.insertSupport(userId, articleId);
        return true;
    }

    // 文章点赞排行榜，取前五
    @Override
    public List<Article> sortBySupport(Integer limit) {
        List<Article> articleList = articleMapper.listBySupport(limit);
        return articleList;
    }

}
